#include <time.h>
#include <stdio.h>                                   
#include <stdlib.h>                                  
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <iostream>
#include <string.h>
#include <sstream>
#include <queue>
#include "GridNode.h"
using namespace std;

/**
 * Choose a random start node
 **/
GridNode* choostStartNode(GridNode** pNodes){
}

/**
 * Choose a random start node
 **/
GridNode* choostGoalNode(GridNode** pNodes, GridNode* pStartNode){
}

//-------------- MAIN -------------------------------
void main(int argc, char *argv[]){

	// Load map
	const char* problemFileName = "../AR0013SR.map";
	const char* heuristicFileName = "../canonicalh_013.dat";
	FILE* problemFilePtr;
	FILE* heuristicFilePtr;
	const int MAX_HEADER_LINE_SIZE = 50;
	char headerLines[MAX_HEADER_LINE_SIZE];
	char filler;
	int height,width;
	problemFilePtr=fopen(problemFileName,"r");
	while(!feof(problemFilePtr)){
		fscanf(problemFilePtr,"%s",headerLines);
		if(strcmp(headerLines,"height")==0){
			fscanf(problemFilePtr,"%d",&height);
		}
		else if(strcmp(headerLines,"width")==0){
			fscanf(problemFilePtr,"%d",&width);
		}
		else if(strcmp(headerLines,"map")==0)
			break;
		printf("%s\n",headerLines);
		memset(headerLines,0,MAX_HEADER_LINE_SIZE);		
	}
	printf("Map dimensions [%d,%d]\n",height,width);
	fscanf(problemFilePtr,"%c*",&filler);
	char** grid = new char*[height];

	int index=0;
	for(int i=0;i<height;i++){
		grid[i]=new char[width];
		for(int j=0;j<width;j++){
			fscanf(problemFilePtr,"%c",&grid[i][j]);
			printf("%c",grid[i][j]);
			if (grid[i][j]!='@'){ // If found an empty space
				++index;
			}
		}
		fscanf(problemFilePtr,"%c",&filler); // This is the end of line character
		if(filler!='\n'){exit(1);}
	}
	printf("----- Start unread part ----------\n"); // SHOULDN'T EVEN EXIST 
	while(feof(problemFilePtr)==false){
		fscanf(problemFilePtr,"%c",&filler);
		printf("%c",filler);
	}
	printf("----- End unread part ----------\n");
	fclose(problemFilePtr);
	
	// Create grid nodes	
	int numOfNodes = index;
	printf("Found %d nodes\n",numOfNodes);
	GridNode** nodes = new GridNode*[numOfNodes];
	index=0;
	for(int i=0;i<height;i++){
		for(int j=0;j<width;j++){
			if (grid[i][j]!='@'){ // If found an empty space
				if(grid[i][j]=='.'){
					nodes[index]=new GridNode(i,j,index);
					++index;
				}
			}
		}
	}

	printf("Hello!");
	// Load heursitic table
	
	int heuristicEntries = numOfNodes*(numOfNodes-1)/2;
	float* heuristic = new float[heuristicEntries];
	heuristicFilePtr=fopen(heuristicFileName,"r");
	index=0;
	while(feof(heuristicFilePtr)==false){
		fscanf(heuristicFilePtr,"%f",&heuristic[index]);
		++index;
		if((index % 1000000)==0)
			printf("---------- Read %d values\n",index);
	}
	fclose(heuristicFilePtr);

	printf("OK!");

	// Run algorithms
	GridNode* startNode = chooseStartNode(nodes);
	GridNode* goalNode = chooseGoalNode(startNode,nodes);
	GridRunner algorithmRunner();

	algorithmRunner.run(startNode,goalNode);

	// delete memory
	for(int i=0;i<height;i++){
		delete[] grid[i];
	}
	delete[] grid;
	delete[] nodes;
}